import java.util.Iterator;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

public class Utils {
	public static int calWidth(int length, int fontSize) {
		if (length == 1) {
			return 30;
		}
		if (fontSize == 10) {
			if ((length * 7 + 8) % 5 != 0) {
				return (length * 7 + 8) + 5 + 5 - (length * 7 + 8 + 5) % 5;
			} else {
				return (length * 7 + 8) + 5;
			}
		}
		if (fontSize == 11) {
			int temp = length * 8 + 8 + 5;
			if (temp % 5 == 0) {
				return temp;
			} else if ((temp % 5) < 3) {
				return temp - temp % 5;
			} else {
				return temp + 5 - temp % 5;
			}
		}
		return length * fontSize;
	}

	public static void logFine(Table table, TableItem item, String proper,
			String[] message, String type) {
		TableItem temp = new TableItem(table, SWT.NONE);
		temp.setText(0, String.valueOf(table.getItemCount()));
		temp.setText(1, message[0]);
		temp.setText(2, message[1]);
		temp.setText(3, message[2]);
		if (message.length >= 4) {
			temp.setText(4, message[3]);
		}
		if (type.equals(Const.ERROR)) {
			temp.setBackground(Const.RED);
		} else if (type.equals(Const.WARNING)) {
			temp.setBackground(Const.YELLOW);
		}
		temp.setData(Const.TABLEITEM, item);
		if (type.equals(Const.ERROR)) {
			setCellColor(item, proper, Const.RED);
		} else if (type.equals(Const.WARNING)) {
			setCellColor(item, proper, Const.YELLOW);
		}
	}

	public static void setCellColor(TableItem item, String proper, Color color) {
		for (int j = 0; j < item.getParent().getColumnCount(); j++) {
			String property = item.getParent().getColumn(j).getText();
			if (property.equals(proper)) {
				item.setBackground(j, color);
				break;
			}
		}
	}

	public static ItemRules getItemRules(List itemRules, String name) {
		ItemRules tmp = null;
		Iterator ite = itemRules.iterator();
		while (ite.hasNext()) {
			tmp = (ItemRules) ite.next();
			if (tmp.getFieldName().equals(name)) {
				return tmp;
			}
		}
		return null;
	}
}
